#include "TextureUtils.h"

ID3D11Texture2D *TextureUtils::MakeColorTexture(ID3D11Device *myDevice, int width, int height, XMCOLOR color)
{
	ID3D11Texture2D *tex;

	UINT colorUint = (color.a << 6 * 4) + (color.b << 4 * 4) + (color.g << 2 * 4) + color.r;

	int *buf = new int[width*height];

	for(int i=0; i<height; i++)
	{
		for(int j=0; j<width; j++)
		{
			buf[i*width + j] = colorUint;
		}
	}
	
	
	D3D11_SUBRESOURCE_DATA tbsd;
	tbsd.pSysMem = (void *)buf;
	tbsd.SysMemPitch = width*4;
	tbsd.SysMemSlicePitch = width*height*4; // Not needed since this is a 2d texture
	
	D3D11_TEXTURE2D_DESC tdesc;
	tdesc.Width = width;
	tdesc.Height = height;
	tdesc.MipLevels = 1;
	tdesc.ArraySize = 1;

	tdesc.SampleDesc.Count = 1;
	tdesc.SampleDesc.Quality = 0;
	tdesc.Usage = D3D11_USAGE_DEFAULT;
	tdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

	tdesc.CPUAccessFlags = 0;
	tdesc.MiscFlags = 0;

	if(FAILED(myDevice->CreateTexture2D(&tdesc,&tbsd,&tex)))
	{
		return(0);
	}

	delete[] buf;

	return(tex);
}

ID3D11Texture2D *TextureUtils::MakeCheckerBoard(ID3D11Device *myDevice,int w,int h)
{
	ID3D11Texture2D *tex;
	D3D11_TEXTURE2D_DESC tdesc;
	D3D11_SUBRESOURCE_DATA tbsd;

	int *buf = new int[w*h];

	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
		{
			if((i&32)==(j&32))
				buf[i*w+j] = 0x00000000;
			else
				buf[i*w+j] = 0xffffffff;
		}

	tbsd.pSysMem = (void *)buf;
	tbsd.SysMemPitch = w*4;
	tbsd.SysMemSlicePitch = w*h*4; // Not needed since this is a 2d texture

	tdesc.Width = w;
	tdesc.Height = h;
	tdesc.MipLevels = 1;
	tdesc.ArraySize = 1;

	tdesc.SampleDesc.Count = 1;
	tdesc.SampleDesc.Quality = 0;
	tdesc.Usage = D3D11_USAGE_DEFAULT;
	tdesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	tdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

	tdesc.CPUAccessFlags = 0;
	tdesc.MiscFlags = 0;

	if(FAILED(myDevice->CreateTexture2D(&tdesc,&tbsd,&tex)))
		return(0);


	delete[] buf;

	return(tex);
}